package com.wzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzy.domain.ResponseResult;
import com.wzy.domain.entity.Index;
import com.wzy.domain.vo.IndexVO;
import com.wzy.mapper.IndexMapper;
import com.wzy.service.IndexService;
import com.wzy.service.UserCommonService;
import com.wzy.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 */
@Service
@RequiredArgsConstructor
public class IndexServiceImpl extends ServiceImpl<IndexMapper, Index>
        implements IndexService
{
    private final IndexMapper indexMapper;
    private final HttpServletRequest httpServletRequest;
    private final UserCommonService userCommonService;

    /**
     * 提交文章目录索引
     */
    @Override
    public ResponseResult<Void> postArticleIndex(List<Index> directoryIndex)
    {
        userCommonService.checkRole(httpServletRequest);
        // 删除该文章已有索引
        LambdaQueryWrapper<Index> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Index::getArticleId, directoryIndex.get(0).getArticleId());
        remove(queryWrapper);
        // 批量保存
        saveBatch(directoryIndex);
        return ResponseResult.okResult();
    }

    /**
     * 获取文章目录索引
     */
    @Override
    public ResponseResult<List<IndexVO>> getArticleIndex()
    {
        List<Index> indices = indexMapper.selectList(new LambdaQueryWrapper<Index>()
                .orderByDesc(Index::getArticleId));
        List<IndexVO> indexVOS = BeanCopyUtils.copyBeanList(indices, IndexVO.class);
        return ResponseResult.okResult(indexVOS);
    }

}




